import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { GlobalService } from '../../../../cores/services';
import { Warehous } from '../../interfaces/warehous.interface';
import { WarehousService } from '../../services/warehous.service';
import { skipWhile, switchMap } from 'rxjs/operators';

@Component({
    selector: 'app-warehous-table',
    templateUrl: './warehous-table.component.html',
})
export class WarehousableComponent implements OnInit {

    theads = ['#', '仓库编号', '仓库名称', '仓库地址', '仓库电话', '仓库面积', '所属部门', '状态', '操作'];
    theadtwo = ['#', '库区名称', '库区面积', '操作'];
    theadthree = ['#',  '库位名称', '所属库区', '库位层数', '库位体积', '最大承重', '长', '宽', '高', '库位成本', '查看二维码', '操作'];
    search = new SearchParams({ start: '', end: '' });

    list = new Array<Warehous>();
    regionlist = new Array<any>();
    Locationlist = new Array<any>();
    status: any;
    loading = false;
    warehous: Warehous = {};
    pagination = new Pagination();

    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private confirm: ConfirmService,
        private toast: ToastService,
        public global: GlobalService,
        private warehousService: WarehousService,
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/warehous/warehous'))
            .subscribe(() => { this.loadDatas(), this.loadDatasRegion(), this.loadDatasLocationList(); });
        this.loadDatas();
        this.loadDatasRegion();
        this.loadDatasLocationList();
    }

    doSearch() {
        this.pagination.reset();
        this.loadDatas();
        this.loadDatasRegion();
        this.loadDatasLocationList();
    }

    doReset() {
        this.search.clean();
        this.doSearch();
    }
    /**
     * 仓库列表
     */
    loadDatas() {
        this.loading = true;
        this.warehousService.searchGoods(this.pagination, this.search)
            .subscribe({
                next: res => {
                    this.pagination.total = res.datas.total;
                    this.list = res.datas.rows;
                },
                complete: () => this.loading = false
            });
    }
    /**
     *库区列表
     */
    loadDatasRegion() {
        this.loading = true;
        this.warehousService.searchRegion(this.pagination, this.search)
            .subscribe({
                next: res => {
                    this.pagination.totaltwo = res.datas.total;
                    this.regionlist = res.datas.rows;
                },
                complete: () => this.loading = false
            });
    }
    /**
    *库位列表
    */
    loadDatasLocationList() {
        this.loading = true;
        this.warehousService.searchLocationList(this.pagination, this.search)
            .subscribe({
                next: res => {
                    this.pagination.totalthree = res.datas.total;
                    this.Locationlist = res.datas.rows;
                },
                complete: () => this.loading = false
            });
    }
    /**
     *
     * @param id 删除仓库
     */
    confirmDelete(id: number) {
        this.confirm.danger('删除确认', `您确认删除该仓库吗？`)
            .pipe(switchMap<void, ApiData>(() => this.warehousService.deletewarehouse(id)))
            .subscribe(() => {
                this.list.splice(this.list.indexOf(this.warehous), 1);
                this.toast.success('删除成功', `成功删除仓库`);
                this.loadDatas();
            });
    }
    /**
     *
     * @param id 删除库区
     */
    warehouseRegion(id: number) {
        this.confirm.danger('删除确认', `您确认删除该库区吗？`)
            .pipe(switchMap<void, ApiData>(() => this.warehousService.deleteRegion(id)))
            .subscribe(() => {
                this.list.splice(this.list.indexOf(this.warehous), 1);
                this.toast.success('删除成功', `成功删除库区`);
                this.loadDatasRegion();
            });
    }
     /**
     *
     * @param id 删除库位
     */
    wareLocation(id: number) {
        this.confirm.danger('删除确认', `您确认删除该库位吗？`)
            .pipe(switchMap<void, ApiData>(() => this.warehousService.deleteLocation(id)))
            .subscribe(() => {
                this.list.splice(this.list.indexOf(this.warehous), 1);
                this.toast.success('删除成功', `成功删除库位`);
                this.loadDatasLocationList();
            });
    }

}
